package com.cm.cl;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.cm.cl.enums.Category;
import com.cm.cl.enums.Departments;
import com.cm.cl.enums.Emergency;
import com.cm.cl.enums.Process;
import com.cm.cl.enums.Source;

public class Dlg_Create extends JDialog {

	/**
	 * 
	 */
	private final JPanel contentPanel = new JPanel();
	private JTextArea txtDetail;
	private JComboBox cmbEmg;
	private JComboBox cmbTaskCat;
	private JComboBox cmbSource;
	private JComboBox cmbCat;
	private JComboBox cmbDep;
	private JLabel lblID;
	private JTextField txtName;
	private JTextField txtFactor;
	private JTextField txtProposer;
	private JTextField txtHandler;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the dialog.
	 * 
	 * @throws SQLException
	 */
	public void insertItem() throws SQLException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Statement ps = null;
		String insertString = "INSERT INTO `"
				+ Database.getTableName()
				+ "`(`id`,`department`, `category`, `source`, `task_name`, `detail`, `task_category`, `emergency`, `proposer`, `factor`, `handler`) VALUES ("
				+ ("'WL-" + sdf.format(new Date()) + "-"
						+ new Object().hashCode() + "',") + "'"
				+ cmbDep.getSelectedItem().toString() + "'," + "'"
				+ cmbCat.getSelectedItem().toString() + "'," + "'"
				+ cmbSource.getSelectedItem().toString() + "'," + "'"
				+ txtName.getText() + "'," + "'" + txtDetail.getText() + "',"
				+ "'" + cmbTaskCat.getSelectedItem().toString() + "'," + "'"
				+ cmbEmg.getSelectedItem().toString() + "'," + "'"
				+ txtProposer.getText() + "'," + "'" + txtFactor.getText()
				+ "'," + "'" + txtHandler.getText() + "')";
		System.out.println(insertString);
		try {
			ps = Database.getConnection().createStatement();
			int rev = ps.executeUpdate(insertString);
			if (1 == rev)
				JOptionPane.showMessageDialog(null, "插入成功");
		} catch (SQLException e1) {
			e1.printStackTrace();
		} finally {
			if (ps != null)
				ps.close();

		}
	}

	public Dlg_Create() {
		setTitle("新建工单");
		setBounds(100, 100, 644, 496);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("工单编号");
			lblNewLabel.setBounds(33, 38, 54, 15);
			contentPanel.add(lblNewLabel);
		}
		{
			JLabel label = new JLabel("问题概述");
			label.setBounds(33, 84, 54, 15);
			contentPanel.add(label);
		}
		{
			txtName = new JTextField();
			txtName.setBounds(109, 81, 429, 21);
			contentPanel.add(txtName);
			txtName.setColumns(10);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("所在室");
			lblNewLabel_1.setBounds(33, 115, 54, 15);
			contentPanel.add(lblNewLabel_1);
		}

		cmbDep = new JComboBox();
		cmbDep.setModel(new DefaultComboBoxModel(Departments.values()));
		cmbDep.setBounds(109, 112, 166, 21);
		contentPanel.add(cmbDep);

		JLabel lblNewLabel_2 = new JLabel("问题分类");
		lblNewLabel_2.setBounds(320, 115, 54, 15);
		contentPanel.add(lblNewLabel_2);

		cmbCat = new JComboBox();
		cmbCat.setModel(new DefaultComboBoxModel(Category.values()));
		cmbCat.setBounds(384, 112, 154, 21);
		contentPanel.add(cmbCat);

		cmbSource = new JComboBox();
		cmbSource.setModel(new DefaultComboBoxModel(Source.values()));
		cmbSource.setBounds(109, 140, 166, 21);
		contentPanel.add(cmbSource);

		JLabel label = new JLabel("问题来源");
		label.setBounds(33, 143, 54, 15);
		contentPanel.add(label);

		JLabel label_1 = new JLabel("提出人");
		label_1.setBounds(320, 143, 54, 15);
		contentPanel.add(label_1);

		txtProposer = new JTextField();
		txtProposer.setBounds(384, 140, 154, 21);
		contentPanel.add(txtProposer);
		txtProposer.setColumns(10);

		txtHandler = new JTextField();
		txtHandler.setBounds(109, 171, 166, 21);
		contentPanel.add(txtHandler);
		txtHandler.setColumns(10);

		JLabel label_2 = new JLabel("提交人");
		label_2.setBounds(33, 174, 54, 15);
		contentPanel.add(label_2);

		cmbTaskCat = new JComboBox();
		cmbTaskCat.setModel(new DefaultComboBoxModel(Process.values()));
		cmbTaskCat.setBounds(109, 202, 166, 21);
		contentPanel.add(cmbTaskCat);

		JLabel label_3 = new JLabel("任务分类");
		label_3.setBounds(33, 205, 54, 15);
		contentPanel.add(label_3);

		JLabel label_4 = new JLabel("紧急程度");
		label_4.setBounds(320, 205, 54, 15);
		contentPanel.add(label_4);

		cmbEmg = new JComboBox();
		cmbEmg.setModel(new DefaultComboBoxModel(Emergency.values()));
		cmbEmg.setBounds(384, 202, 154, 21);
		contentPanel.add(cmbEmg);

		JLabel label_5 = new JLabel("工单系数");
		label_5.setBounds(33, 236, 54, 15);
		contentPanel.add(label_5);

		txtFactor = new JTextField();
		txtFactor.setBounds(109, 233, 166, 21);
		contentPanel.add(txtFactor);
		txtFactor.setColumns(10);

		JLabel label_6 = new JLabel("详情");
		label_6.setBounds(33, 273, 54, 15);
		contentPanel.add(label_6);

		txtDetail = new JTextArea();
		txtDetail.setLineWrap(true);
		txtDetail.setColumns(1);
		txtDetail.setText("键入详情");
		txtDetail.setRows(5);
		txtDetail.setBounds(33, 298, 505, 104);
		contentPanel.add(txtDetail);

		lblID = new JLabel("");
		lblID.setBounds(109, 38, 54, 15);
		contentPanel.add(lblID);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							insertItem();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Dlg_Create.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
